/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.Model_Answer;
import Util.DataUtil;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TuanDat
 */
public class DAO_Answer {

    public ArrayList<Model_Answer> get_AnswerbyQuestionID(int ID) {
        ArrayList<Model_Answer> listAnswer = new ArrayList<Model_Answer>();
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_GetAnswerbyQuestionID(?)}");
            csmt.setInt("ID", ID);
            ResultSet rs = csmt.executeQuery();
            while (rs.next()) {
                Model_Answer oneAnswer = new Model_Answer();
                oneAnswer.setId(rs.getString("ID"));
                oneAnswer.setQuestion_id(rs.getString("Question_ID"));
                oneAnswer.setContent(rs.getString("Content"));
                oneAnswer.setCorrect(rs.getString("Correct"));
                oneAnswer.setLast_update(rs.getString("Last_Update"));

                listAnswer.add(oneAnswer);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return listAnswer;
    }

    public boolean insertAnswer(Model_Answer answer) {
        boolean result = false;
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_insertAnswer(?,?,?)}");
            csmt.setString("Question_ID", answer.getQuestion_id());
            csmt.setString("Content", answer.getContent());
            csmt.setString("Correct", answer.getCorrect());

            csmt.execute();
            result = true;
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Question.class.getName()).log(Level.SEVERE, null, ex);
            result = false;
        }

        return result;
    }
    /*
 * funtion Update Answer where question
 * and open the template in the editor.
 */
    public Integer Updatet_Answer(Model_Answer answer) {
        Integer result =0;
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_UpdateAnswer(?,?,?,?,?)}");
            csmt.setString("ID", answer.getId());
            csmt.setString("Question_ID", answer.getQuestion_id());
            csmt.setString("Content", answer.getContent());
            csmt.setString("Correct", answer.getCorrect());
            csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);
            csmt.execute();
            result=csmt.getInt("RESULT");
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Question.class.getName()).log(Level.SEVERE, null, ex);
            result = 0;
        }

        return result;
    }
/*
 * funtion delete Answer where question
 * and open the template in the editor.
 */
    public Integer Delete_Answer(String id) {
        Integer result = 0;
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{Call sp_deleteAnswerByQuestionID(?,?)}");
            csmt.setString("Question_ID", id);
            csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);
            csmt.execute();
            result = csmt.getInt("RESULT");
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Category_Question.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
